package com.kaishengit.web;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.kaishengit.entity.Idea;
import com.kaishengit.entity.IdeaComment;
import com.kaishengit.entity.User;
import com.kaishengit.service.IdeaCommentService;
import com.kaishengit.service.IdeaService;
import com.kaishengit.util.PkTime;
import com.kaishengit.util.PkUuid;
import com.kaishengit.util.TimeUtil;

public class IdeaCommentServlet extends HttpServlet {

	private static final long serialVersionUID = 1L;

	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		String id=request.getParameter("id");
		IdeaService service=new IdeaService();
		Idea idea=service.findById(id);
		request.setAttribute("idea", idea);
		request.getRequestDispatcher("/WEB-INF/views/ideacomment.jsp").forward(request, response);
	}

	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		String ideaId=request.getParameter("id");
		HttpSession session=request.getSession();
		User user=(User)session.getAttribute("user");
		String userId=user.getId();
		String content=request.getParameter("comment");
		PkUuid i=new PkUuid();
		String id=i.getPk();
		PkTime t=new PkTime();
		String ct=t.getTime();
		
		IdeaComment ic=new IdeaComment();
		ic.setId(id);
		ic.setCreatetime(ct);
		ic.setContent(content);
		ic.setIdeaId(ideaId);
		ic.setUserId(userId);
		
		IdeaCommentService service=new IdeaCommentService();
		service.save(ic);
		response.sendRedirect("idea.jspx");
	}

}
